using System;
using System.Data;
using System.Data.OleDb;
using System.Collections;
using System.Configuration;
using PCSComUtils.DataAccess;
using PCSComUtils.PCSExc;
using PCSComUtils.Common;
using System.Runtime.InteropServices;

namespace PCSComMaterials.Inventory.DS
{
	[Guid("B678BCD3-B15F-4fc7-9B7C-8E5D84F3015E")]
	public class IV_ItemDetailDS 
	{
		public IV_ItemDetailDS()
		{
		}
		private const string THIS = "PCSComMaterials.Inventory.DS.IV_ItemDetailDS";

	
		//**************************************************************************              
		///    <Description>
		///       This method uses to add data to IV_ItemDetail
		///    </Description>
		///    <Inputs>
		///        IV_ItemDetailVO       
		///    </Inputs>
		///    <Outputs>
		///       newly inserted primarkey value
		///    </Outputs>
		///    <Returns>
		///       void
		///    </Returns>
		///    <Authors>
		///       HungLa
		///    </Authors>
		///    <History>
		///       Wednesday, February 23, 2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************


		public void Add(object pobjObjectVO)
		{
			const string METHOD_NAME = THIS + ".Add()";
			 
			OleDbConnection oconPCS =null;
			OleDbCommand ocmdPCS =null;
			try
			{
				IV_ItemDetailVO objObject = (IV_ItemDetailVO) pobjObjectVO;
				string strSql = String.Empty;
				
				oconPCS = new OleDbConnection(Utils.Instance.OleDbConnectionString);
				ocmdPCS = new OleDbCommand("", oconPCS);
				
				strSql=	"INSERT INTO IV_ItemDetail("
				+ IV_ItemDetailTable.OHQUANTITY_FLD + ","
				+ IV_ItemDetailTable.INSPSTATUS_FLD + ","
				+ IV_ItemDetailTable.MRBDISPLAY_FLD + ","
				+ IV_ItemDetailTable.CCNID_FLD + ","
				+ IV_ItemDetailTable.PRODUCTID_FLD + ","
				+ IV_ItemDetailTable.MASTERLOCATIONID_FLD + ","
				+ IV_ItemDetailTable.LOCATIONID_FLD + ","
				+ IV_ItemDetailTable.BINID_FLD + ","
				+ IV_ItemDetailTable.LOT_FLD + ","
				+ IV_ItemDetailTable.SERIAL_FLD + ")"
				+ "VALUES(?,?,?,?,?,?,?,?,?,?)";

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_ItemDetailTable.OHQUANTITY_FLD, OleDbType.Decimal));
				ocmdPCS.Parameters[IV_ItemDetailTable.OHQUANTITY_FLD].Value = objObject.OHQuantity;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_ItemDetailTable.INSPSTATUS_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[IV_ItemDetailTable.INSPSTATUS_FLD].Value = objObject.InspStatus;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_ItemDetailTable.MRBDISPLAY_FLD, OleDbType.VarChar));
				ocmdPCS.Parameters[IV_ItemDetailTable.MRBDISPLAY_FLD].Value = objObject.MRBDisplay;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_ItemDetailTable.CCNID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[IV_ItemDetailTable.CCNID_FLD].Value = objObject.CCNID;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_ItemDetailTable.PRODUCTID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[IV_ItemDetailTable.PRODUCTID_FLD].Value = objObject.ProductID;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_ItemDetailTable.MASTERLOCATIONID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[IV_ItemDetailTable.MASTERLOCATIONID_FLD].Value = objObject.MasterLocationID;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_ItemDetailTable.LOCATIONID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[IV_ItemDetailTable.LOCATIONID_FLD].Value = objObject.LocationID;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_ItemDetailTable.BINID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[IV_ItemDetailTable.BINID_FLD].Value = objObject.BinID;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_ItemDetailTable.LOT_FLD, OleDbType.VarWChar));
				ocmdPCS.Parameters[IV_ItemDetailTable.LOT_FLD].Value = objObject.Lot;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_ItemDetailTable.SERIAL_FLD, OleDbType.VarWChar));
				ocmdPCS.Parameters[IV_ItemDetailTable.SERIAL_FLD].Value = objObject.Serial;


				
				ocmdPCS.CommandText = strSql;
				ocmdPCS.Connection.Open();
				ocmdPCS.ExecuteNonQuery();	

			}
			catch(OleDbException ex)
			{
				if (ex.Errors[1].NativeError == ErrorCode.SQLDUPLICATE_KEYCODE)
				{
																   
					throw new PCSDBException(ErrorCode.DUPLICATE_KEY, METHOD_NAME, ex);				
				}
				else
				{
					throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME,ex);
				}
			}			

			catch(InvalidOperationException ex)
			{
				throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME,ex);
			}
			catch (Exception ex) 
			{
				throw new PCSDBException(ErrorCode.OTHER_ERROR, METHOD_NAME, ex);
			}

			finally 
			{
				if (oconPCS!=null) 
				{
					if (oconPCS.State != ConnectionState.Closed) 
					{
						oconPCS.Close();
					}
				}
			}
		}
	


		//**************************************************************************              
		///    <Description>
		///       This method uses to add data to IV_ItemDetail
		///    </Description>
		///    <Inputs>
		///        IV_ItemDetailVO       
		///    </Inputs>
		///    <Outputs>
		///       newly inserted primarkey value
		///    </Outputs>
		///    <Returns>
		///       void
		///    </Returns>
		///    <Authors>
		///       HungLa
		///    </Authors>
		///    <History>
		///       Wednesday, February 23, 2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************


		public void AddReturnedGoods(object pobjObjectVO)
		{
			const string METHOD_NAME = THIS + ".Add()";
			 
			OleDbConnection oconPCS =null;
			OleDbCommand ocmdPCS =null;
			try
			{
				IV_ItemDetailVO objObject = (IV_ItemDetailVO) pobjObjectVO;
				string strSql = String.Empty;
				
				oconPCS = new OleDbConnection(Utils.Instance.OleDbConnectionString);
				ocmdPCS = new OleDbCommand("", oconPCS);
				
				strSql=	"INSERT INTO IV_ItemDetail("
					+ IV_ItemDetailTable.OHQUANTITY_FLD + ","
					+ IV_ItemDetailTable.INSPSTATUS_FLD + ","
					+ IV_ItemDetailTable.MRBDISPLAY_FLD + ","
					+ IV_ItemDetailTable.CCNID_FLD + ","
					+ IV_ItemDetailTable.PRODUCTID_FLD + ","
					+ IV_ItemDetailTable.MASTERLOCATIONID_FLD + ","
					+ IV_ItemDetailTable.LOCATIONID_FLD + ","
					+ IV_ItemDetailTable.BINID_FLD + ","
					+ IV_ItemDetailTable.LOT_FLD + ","
					+ IV_ItemDetailTable.SERIAL_FLD + ")"
					+ "VALUES(?,?,?,?,?,?,?,?,?,?)";

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_ItemDetailTable.OHQUANTITY_FLD, OleDbType.Decimal));
				ocmdPCS.Parameters[IV_ItemDetailTable.OHQUANTITY_FLD].Value = objObject.OHQuantity;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_ItemDetailTable.INSPSTATUS_FLD, OleDbType.Integer));
				if (objObject.InspStatus>0) 
				{
					ocmdPCS.Parameters[IV_ItemDetailTable.INSPSTATUS_FLD].Value = objObject.InspStatus;
				}
				else
				{
					ocmdPCS.Parameters[IV_ItemDetailTable.INSPSTATUS_FLD].Value = DBNull.Value;
				}

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_ItemDetailTable.MRBDISPLAY_FLD, OleDbType.VarChar));
				ocmdPCS.Parameters[IV_ItemDetailTable.MRBDISPLAY_FLD].Value = objObject.MRBDisplay;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_ItemDetailTable.CCNID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[IV_ItemDetailTable.CCNID_FLD].Value = objObject.CCNID;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_ItemDetailTable.PRODUCTID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[IV_ItemDetailTable.PRODUCTID_FLD].Value = objObject.ProductID;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_ItemDetailTable.MASTERLOCATIONID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[IV_ItemDetailTable.MASTERLOCATIONID_FLD].Value = objObject.MasterLocationID;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_ItemDetailTable.LOCATIONID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[IV_ItemDetailTable.LOCATIONID_FLD].Value = objObject.LocationID;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_ItemDetailTable.BINID_FLD, OleDbType.Integer));
				if (objObject.BinID > 0)
				{
					ocmdPCS.Parameters[IV_ItemDetailTable.BINID_FLD].Value = objObject.BinID;
				}
				else
				{
					ocmdPCS.Parameters[IV_ItemDetailTable.BINID_FLD].Value = DBNull.Value;
				}

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_ItemDetailTable.LOT_FLD, OleDbType.VarWChar));
				ocmdPCS.Parameters[IV_ItemDetailTable.LOT_FLD].Value = objObject.Lot;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_ItemDetailTable.SERIAL_FLD, OleDbType.VarWChar));
				ocmdPCS.Parameters[IV_ItemDetailTable.SERIAL_FLD].Value = objObject.Serial;

				ocmdPCS.CommandText = strSql;
				ocmdPCS.Connection.Open();
				ocmdPCS.ExecuteNonQuery();	

			}
			catch(OleDbException ex)
			{
				if (ex.Errors[1].NativeError == ErrorCode.SQLDUPLICATE_KEYCODE)
				{
																   
					throw new PCSDBException(ErrorCode.DUPLICATE_KEY, METHOD_NAME, ex);				
				}
				else
				{
					throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME,ex);
				}
			}			

			catch(InvalidOperationException ex)
			{
				throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME,ex);
			}
			catch (Exception ex) 
			{
				throw new PCSDBException(ErrorCode.OTHER_ERROR, METHOD_NAME, ex);
			}

			finally 
			{
				if (oconPCS!=null) 
				{
					if (oconPCS.State != ConnectionState.Closed) 
					{
						oconPCS.Close();
					}
				}
			}
		}
	
	

		//**************************************************************************              
		///    <Description>
		///       This method uses to delete data from IV_ItemDetail
		///    </Description>
		///    <Inputs>
		///        ID       
		///    </Inputs>
		///    <Outputs>
		///       void
		///    </Outputs>
		///    <Returns>
		///       
		///    </Returns>
		///    <Authors>
		///       HungLa
		///    </Authors>
		///    <History>
		///       09-Dec-2004
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************

		public void Delete(int pintID)
		{
			const string METHOD_NAME = THIS + ".Delete()";
			string strSql = String.Empty;
			strSql=	"DELETE " + IV_ItemDetailTable.TABLE_NAME + " WHERE  " + "ItemDetailID" + "=" + pintID.ToString();
			OleDbConnection oconPCS=null;
			OleDbCommand ocmdPCS =null;
			try
			{
				
				oconPCS = new OleDbConnection(Utils.Instance.OleDbConnectionString);
				ocmdPCS = new OleDbCommand(strSql, oconPCS);

				ocmdPCS.Connection.Open();
				ocmdPCS.ExecuteNonQuery();	
				ocmdPCS = null;

			}
			catch(OleDbException ex)
			{
				if (ex.Errors[1].NativeError == ErrorCode.SQLCASCADE_PREVENT_KEYCODE)
				{
																   
					throw new PCSDBException(ErrorCode.CASCADE_DELETE_PREVENT, METHOD_NAME, ex);				
				}
				else
				{
					throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME,ex);
				}
			}			

			catch (Exception ex) 
			{
				throw new PCSDBException(ErrorCode.OTHER_ERROR, METHOD_NAME, ex);
			}



			finally 
			{
				if (oconPCS!=null) 
				{
					if (oconPCS.State != ConnectionState.Closed) 
					{
						oconPCS.Close();
					}
				}
			}
		}
	

	

		//**************************************************************************              
		///    <Description>
		///       This method uses to get data from IV_ItemDetail
		///    </Description>
		///    <Inputs>
		///        ID       
		///    </Inputs>
		///    <Outputs>
		///       IV_ItemDetailVO
		///    </Outputs>
		///    <Returns>
		///       IV_ItemDetailVO
		///    </Returns>
		///    <Authors>
		///       HungLa
		///    </Authors>
		///    <History>
		///       Wednesday, February 23, 2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************

		public object GetObjectVO(int pintID)
		{
			const string METHOD_NAME = THIS + ".GetObjectVO()";
			DataSet dstPCS = new DataSet();
			
			OleDbDataReader odrPCS = null;
			OleDbConnection oconPCS = null;
			OleDbCommand ocmdPCS = null;
			try 
			{
				string strSql = String.Empty;
				strSql=	"SELECT "
				+ IV_ItemDetailTable.ITEMDETAILID_FLD + ","
				+ IV_ItemDetailTable.OHQUANTITY_FLD + ","
				+ IV_ItemDetailTable.INSPSTATUS_FLD + ","
				+ IV_ItemDetailTable.MRBDISPLAY_FLD + ","
				+ IV_ItemDetailTable.CCNID_FLD + ","
				+ IV_ItemDetailTable.PRODUCTID_FLD + ","
				+ IV_ItemDetailTable.MASTERLOCATIONID_FLD + ","
				+ IV_ItemDetailTable.LOCATIONID_FLD + ","
				+ IV_ItemDetailTable.BINID_FLD + ","
				+ IV_ItemDetailTable.LOT_FLD + ","
				+ IV_ItemDetailTable.SERIAL_FLD
				+ " FROM " + IV_ItemDetailTable.TABLE_NAME
				+" WHERE " + IV_ItemDetailTable.ITEMDETAILID_FLD + "=" + pintID;

				
				oconPCS = new OleDbConnection(Utils.Instance.OleDbConnectionString);
				ocmdPCS = new OleDbCommand(strSql, oconPCS);

				ocmdPCS.Connection.Open();
				odrPCS = ocmdPCS.ExecuteReader();

				IV_ItemDetailVO objObject = new IV_ItemDetailVO();

				while (odrPCS.Read())
				{ 
				objObject.ItemDetailID = int.Parse(odrPCS[IV_ItemDetailTable.ITEMDETAILID_FLD].ToString().Trim());
				objObject.OHQuantity = Decimal.Parse(odrPCS[IV_ItemDetailTable.OHQUANTITY_FLD].ToString().Trim());
				objObject.InspStatus = int.Parse(odrPCS[IV_ItemDetailTable.INSPSTATUS_FLD].ToString().Trim());
				objObject.MRBDisplay = odrPCS[IV_ItemDetailTable.MRBDISPLAY_FLD].ToString().Trim();
				objObject.CCNID = int.Parse(odrPCS[IV_ItemDetailTable.CCNID_FLD].ToString().Trim());
				objObject.ProductID = int.Parse(odrPCS[IV_ItemDetailTable.PRODUCTID_FLD].ToString().Trim());
				objObject.MasterLocationID = int.Parse(odrPCS[IV_ItemDetailTable.MASTERLOCATIONID_FLD].ToString().Trim());
				objObject.LocationID = int.Parse(odrPCS[IV_ItemDetailTable.LOCATIONID_FLD].ToString().Trim());
				objObject.BinID = int.Parse(odrPCS[IV_ItemDetailTable.BINID_FLD].ToString().Trim());
				objObject.Lot = odrPCS[IV_ItemDetailTable.LOT_FLD].ToString().Trim();
				objObject.Serial = odrPCS[IV_ItemDetailTable.SERIAL_FLD].ToString().Trim();

				}		
				return objObject;					
			}
			catch(OleDbException ex)
			{			
				throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME,ex);
			}			

			catch (Exception ex) 
			{
				throw new PCSDBException(ErrorCode.OTHER_ERROR, METHOD_NAME, ex);
			}

			finally 
			{
				if (oconPCS!=null) 
				{
					if (oconPCS.State != ConnectionState.Closed) 
					{
						oconPCS.Close();
					}
				}
			}
		}


		//**************************************************************************              
		///    <Description>
		///       This method uses to get data from IV_ItemDetail
		///    </Description>
		///    <Inputs>
		///        ID       
		///    </Inputs>
		///    <Outputs>
		///       IV_ItemDetailVO
		///    </Outputs>
		///    <Returns>
		///       IV_ItemDetailVO
		///    </Returns>
		///    <Authors>
		///       HungLa
		///    </Authors>
		///    <History>
		///       Wednesday, February 23, 2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************

		public bool HasProductID (int pintProductID,
								int pintMasterLocationID,
								int pintLocationID,
								int pintBinID,
								int pintCCNID,
								int pintInspStatus,
								string strMRBDisplay,
								string strLot,
								string strSerial)
		{
			const string METHOD_NAME = THIS + ".HasProductID()";
			DataSet dstPCS = new DataSet();
			
			OleDbConnection oconPCS = null;
			OleDbCommand ocmdPCS = null;
			try 
			{
				string strSql = String.Empty;

				strSql=	"SELECT Count(*)"
					+ " FROM " + IV_ItemDetailTable.TABLE_NAME
					+" WHERE " + IV_ItemDetailTable.PRODUCTID_FLD + "= ?" 
					+" AND " + IV_ItemDetailTable.MASTERLOCATIONID_FLD + "= ?" 
					+" AND " + IV_ItemDetailTable.LOCATIONID_FLD + "= ?" 
					+" AND " + IV_ItemDetailTable.CCNID_FLD + "= ?" 
					+" AND " + IV_ItemDetailTable.MRBDISPLAY_FLD + "= ?" 
					+" AND " + IV_ItemDetailTable.LOT_FLD + "= ?" 
					+" AND " + IV_ItemDetailTable.SERIAL_FLD + "= ?" ;
				if (pintBinID > 0) 
				{
					strSql += " AND " + IV_ItemDetailTable.BINID_FLD + "= "  + pintBinID.ToString();
				}
				else
				{
					strSql += " AND " + IV_ItemDetailTable.BINID_FLD + " IS NULL " ;
				}
				if (pintInspStatus > 0)
				{
					strSql += " AND " + IV_ItemDetailTable.INSPSTATUS_FLD + "= "  + pintInspStatus.ToString();
				}
				else
				{
					strSql += " AND " + IV_ItemDetailTable.INSPSTATUS_FLD + " IS NULL ";
				}
					
				
				oconPCS = new OleDbConnection(Utils.Instance.OleDbConnectionString);
				ocmdPCS = new OleDbCommand(strSql, oconPCS);

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_ItemDetailTable.PRODUCTID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[IV_ItemDetailTable.PRODUCTID_FLD].Value = pintProductID;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_ItemDetailTable.MASTERLOCATIONID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[IV_ItemDetailTable.MASTERLOCATIONID_FLD].Value = pintMasterLocationID;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_ItemDetailTable.LOCATIONID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[IV_ItemDetailTable.LOCATIONID_FLD].Value = pintLocationID;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_ItemDetailTable.CCNID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[IV_ItemDetailTable.CCNID_FLD].Value = pintCCNID;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_ItemDetailTable.MRBDISPLAY_FLD, OleDbType.VarChar));
				ocmdPCS.Parameters[IV_ItemDetailTable.MRBDISPLAY_FLD].Value = strMRBDisplay;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_ItemDetailTable.LOT_FLD, OleDbType.VarWChar));
				ocmdPCS.Parameters[IV_ItemDetailTable.LOT_FLD].Value = strLot;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_ItemDetailTable.SERIAL_FLD, OleDbType.VarWChar));
				ocmdPCS.Parameters[IV_ItemDetailTable.SERIAL_FLD].Value = strSerial;


				ocmdPCS.Connection.Open();

				int intResult = int.Parse (ocmdPCS.ExecuteScalar().ToString());
				if (intResult <= 0)
				{
					return false;
				}
				else
				{
					return true;
				}
			}
			catch(OleDbException ex)
			{			
				throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME,ex);
			}			

			catch (Exception ex) 
			{
				throw new PCSDBException(ErrorCode.OTHER_ERROR, METHOD_NAME, ex);
			}

			finally 
			{
				if (oconPCS!=null) 
				{
					if (oconPCS.State != ConnectionState.Closed) 
					{
						oconPCS.Close();
					}
				}
			}
		}


		//**************************************************************************              
		///    <Description>
		///       This method uses to update data to IV_ItemDetail
		///    </Description>
		///    <Inputs>
		///       IV_ItemDetailVO       
		///    </Inputs>
		///    <Outputs>
		///       
		///    </Outputs>
		///    <Returns>
		///       
		///    </Returns>
		///    <Authors>
		///       HungLa
		///    </Authors>
		///    <History>
		///       09-Dec-2004
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		
	
		public void Update(object pobjObjecVO)
		{
			const string METHOD_NAME = THIS + ".Update()";

			IV_ItemDetailVO objObject = (IV_ItemDetailVO) pobjObjecVO;
			

			//prepare value for parameters
			OleDbConnection oconPCS =null;
			OleDbCommand ocmdPCS = null;
			try 
			{
				string strSql = String.Empty;
				
				oconPCS = new OleDbConnection(Utils.Instance.OleDbConnectionString);
				ocmdPCS = new OleDbCommand(strSql, oconPCS);
				strSql=	"UPDATE IV_ItemDetail SET "
				+ IV_ItemDetailTable.OHQUANTITY_FLD + "=   ?" + ","
				+ IV_ItemDetailTable.INSPSTATUS_FLD + "=   ?" + ","
				+ IV_ItemDetailTable.MRBDISPLAY_FLD + "=   ?" + ","
				+ IV_ItemDetailTable.CCNID_FLD + "=   ?" + ","
				+ IV_ItemDetailTable.PRODUCTID_FLD + "=   ?" + ","
				+ IV_ItemDetailTable.MASTERLOCATIONID_FLD + "=   ?" + ","
				+ IV_ItemDetailTable.LOCATIONID_FLD + "=   ?" + ","
				+ IV_ItemDetailTable.BINID_FLD + "=   ?" + ","
				+ IV_ItemDetailTable.LOT_FLD + "=   ?" + ","
				+ IV_ItemDetailTable.SERIAL_FLD + "=  ?"
				+" WHERE " + IV_ItemDetailTable.ITEMDETAILID_FLD + "= ?";

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_ItemDetailTable.OHQUANTITY_FLD, OleDbType.Decimal));
				ocmdPCS.Parameters[IV_ItemDetailTable.OHQUANTITY_FLD].Value = objObject.OHQuantity;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_ItemDetailTable.INSPSTATUS_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[IV_ItemDetailTable.INSPSTATUS_FLD].Value = objObject.InspStatus;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_ItemDetailTable.MRBDISPLAY_FLD, OleDbType.VarChar));
				ocmdPCS.Parameters[IV_ItemDetailTable.MRBDISPLAY_FLD].Value = objObject.MRBDisplay;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_ItemDetailTable.CCNID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[IV_ItemDetailTable.CCNID_FLD].Value = objObject.CCNID;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_ItemDetailTable.PRODUCTID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[IV_ItemDetailTable.PRODUCTID_FLD].Value = objObject.ProductID;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_ItemDetailTable.MASTERLOCATIONID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[IV_ItemDetailTable.MASTERLOCATIONID_FLD].Value = objObject.MasterLocationID;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_ItemDetailTable.LOCATIONID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[IV_ItemDetailTable.LOCATIONID_FLD].Value = objObject.LocationID;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_ItemDetailTable.BINID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[IV_ItemDetailTable.BINID_FLD].Value = objObject.BinID;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_ItemDetailTable.LOT_FLD, OleDbType.VarWChar));
				ocmdPCS.Parameters[IV_ItemDetailTable.LOT_FLD].Value = objObject.Lot;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_ItemDetailTable.SERIAL_FLD, OleDbType.VarWChar));
				ocmdPCS.Parameters[IV_ItemDetailTable.SERIAL_FLD].Value = objObject.Serial;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_ItemDetailTable.ITEMDETAILID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[IV_ItemDetailTable.ITEMDETAILID_FLD].Value = objObject.ItemDetailID;


				ocmdPCS.CommandText = strSql;
				ocmdPCS.Connection.Open();
				ocmdPCS.ExecuteNonQuery();	
			}
			catch(OleDbException ex)
			{
				if (ex.Errors[1].NativeError == ErrorCode.SQLDUPLICATE_KEYCODE)
				{
																   
					throw new PCSDBException(ErrorCode.DUPLICATE_KEY, METHOD_NAME, ex);				
				}
				else
				{
					throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME,ex);
				}
			}			

			catch(InvalidOperationException ex)
			{
				throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME,ex);
			}
			catch (Exception ex) 
			{
				throw new PCSDBException(ErrorCode.OTHER_ERROR, METHOD_NAME, ex);
			}

			finally 
			{
				if (oconPCS!=null) 
				{
					if (oconPCS.State != ConnectionState.Closed) 
					{
						oconPCS.Close();
					}
				}
			}

		}


		//**************************************************************************              
		///    <Description>
		///       This method uses to update data to IV_ItemDetail
		///    </Description>
		///    <Inputs>
		///       IV_ItemDetailVO       
		///    </Inputs>
		///    <Outputs>
		///       
		///    </Outputs>
		///    <Returns>
		///       
		///    </Returns>
		///    <Authors>
		///       HungLa
		///    </Authors>
		///    <History>
		///       09-Dec-2004
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		
	
		public void UpdateReturnedGoods(object pobjObjecVO)
		{
			const string METHOD_NAME = THIS + ".UpdateReturnedGoods()";

			IV_ItemDetailVO objObject = (IV_ItemDetailVO) pobjObjecVO;
			

			//prepare value for parameters
			OleDbConnection oconPCS =null;
			OleDbCommand ocmdPCS = null;
			try 
			{
				string strSql = String.Empty;
				
				oconPCS = new OleDbConnection(Utils.Instance.OleDbConnectionString);
				ocmdPCS = new OleDbCommand(strSql, oconPCS);
				strSql=	"UPDATE IV_ItemDetail SET "
					+ IV_ItemDetailTable.OHQUANTITY_FLD + "=" + IV_ItemDetailTable.OHQUANTITY_FLD +  " + ?" 
					+" WHERE "   + IV_ItemDetailTable.MRBDISPLAY_FLD + "=   ?" 
					+" AND "   + IV_ItemDetailTable.CCNID_FLD + "=   ?" 
					+" AND "   + IV_ItemDetailTable.PRODUCTID_FLD + "=   ?" 
					+" AND "   + IV_ItemDetailTable.MASTERLOCATIONID_FLD + "=   ?" 
					+" AND "   + IV_ItemDetailTable.LOCATIONID_FLD + "=   ?" 
					+" AND "   + IV_ItemDetailTable.LOT_FLD + "=   ?" 
					+" AND "   + IV_ItemDetailTable.SERIAL_FLD + "=  ?";
				if (objObject.InspStatus > 0)
				{
					strSql +=  " AND " + IV_ItemDetailTable.INSPSTATUS_FLD + "="  + objObject.InspStatus.ToString();
				}
				else
				{
					strSql +=  " AND " + IV_ItemDetailTable.INSPSTATUS_FLD + " IS NULL" ;
				}
				if (objObject.BinID > 0)
				{
					strSql +=  " AND " + IV_ItemDetailTable.BINID_FLD + "="  + objObject.BinID.ToString();
				}
				else
				{
					strSql +=  " AND " + IV_ItemDetailTable.BINID_FLD + " IS NULL" ;
				}

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_ItemDetailTable.OHQUANTITY_FLD, OleDbType.Decimal));
				ocmdPCS.Parameters[IV_ItemDetailTable.OHQUANTITY_FLD].Value = objObject.OHQuantity;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_ItemDetailTable.MRBDISPLAY_FLD, OleDbType.VarChar));
				ocmdPCS.Parameters[IV_ItemDetailTable.MRBDISPLAY_FLD].Value = objObject.MRBDisplay;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_ItemDetailTable.CCNID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[IV_ItemDetailTable.CCNID_FLD].Value = objObject.CCNID;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_ItemDetailTable.PRODUCTID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[IV_ItemDetailTable.PRODUCTID_FLD].Value = objObject.ProductID;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_ItemDetailTable.MASTERLOCATIONID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[IV_ItemDetailTable.MASTERLOCATIONID_FLD].Value = objObject.MasterLocationID;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_ItemDetailTable.LOCATIONID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[IV_ItemDetailTable.LOCATIONID_FLD].Value = objObject.LocationID;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_ItemDetailTable.LOT_FLD, OleDbType.VarWChar));
				ocmdPCS.Parameters[IV_ItemDetailTable.LOT_FLD].Value = objObject.Lot;

				ocmdPCS.Parameters.Add(new OleDbParameter(IV_ItemDetailTable.SERIAL_FLD, OleDbType.VarWChar));
				ocmdPCS.Parameters[IV_ItemDetailTable.SERIAL_FLD].Value = objObject.Serial;

				ocmdPCS.CommandText = strSql;
				ocmdPCS.Connection.Open();
				ocmdPCS.ExecuteNonQuery();	
			}
			catch(OleDbException ex)
			{
				if (ex.Errors[1].NativeError == ErrorCode.SQLDUPLICATE_KEYCODE)
				{
																   
					throw new PCSDBException(ErrorCode.DUPLICATE_KEY, METHOD_NAME, ex);				
				}
				else
				{
					throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME,ex);
				}
			}			

			catch(InvalidOperationException ex)
			{
				throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME,ex);
			}
			catch (Exception ex) 
			{
				throw new PCSDBException(ErrorCode.OTHER_ERROR, METHOD_NAME, ex);
			}

			finally 
			{
				if (oconPCS!=null) 
				{
					if (oconPCS.State != ConnectionState.Closed) 
					{
						oconPCS.Close();
					}
				}
			}

		}


		//**************************************************************************              
		///    <Description>
		///       This method uses to get all data from IV_ItemDetail
		///    </Description>
		///    <Inputs>
		///               
		///    </Inputs>
		///    <Outputs>
		///       DataSet
		///    </Outputs>
		///    <Returns>
		///       DataSet
		///    </Returns>
		///    <Authors>
		///       HungLa
		///    </Authors>
		///    <History>
		///       Wednesday, February 23, 2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************

		public DataSet List()
		{
			const string METHOD_NAME = THIS + ".List()";
			DataSet dstPCS = new DataSet();
			
		

			OleDbConnection oconPCS =null;
			OleDbCommand ocmdPCS = null;
			try 
			{
				string strSql = String.Empty;
				
				strSql=	"SELECT "
				+ IV_ItemDetailTable.ITEMDETAILID_FLD + ","
				+ IV_ItemDetailTable.OHQUANTITY_FLD + ","
				+ IV_ItemDetailTable.INSPSTATUS_FLD + ","
				+ IV_ItemDetailTable.MRBDISPLAY_FLD + ","
				+ IV_ItemDetailTable.CCNID_FLD + ","
				+ IV_ItemDetailTable.PRODUCTID_FLD + ","
				+ IV_ItemDetailTable.MASTERLOCATIONID_FLD + ","
				+ IV_ItemDetailTable.LOCATIONID_FLD + ","
				+ IV_ItemDetailTable.BINID_FLD + ","
				+ IV_ItemDetailTable.LOT_FLD + ","
				+ IV_ItemDetailTable.SERIAL_FLD
					+ " FROM " + IV_ItemDetailTable.TABLE_NAME;
				
				oconPCS = new OleDbConnection(Utils.Instance.OleDbConnectionString);
				ocmdPCS = new OleDbCommand(strSql, oconPCS);
				ocmdPCS.Connection.Open();
				
				OleDbDataAdapter odadPCS = new OleDbDataAdapter(ocmdPCS);
				odadPCS.Fill(dstPCS,IV_ItemDetailTable.TABLE_NAME);

				return dstPCS;
			}
			catch(OleDbException ex)
			{
   				throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME,ex);
			}			
			catch (Exception ex) 
			{
				throw new PCSDBException(ErrorCode.OTHER_ERROR, METHOD_NAME, ex);
			}

			finally 
			{
				if (oconPCS!=null) 
				{
					if (oconPCS.State != ConnectionState.Closed) 
					{
						oconPCS.Close();
					}
				}
			}


		}


		//**************************************************************************              
		///    <Description>
		///       This method uses to update a DataSet
		///    </Description>
		///    <Inputs>
		///        DataSet       
		///    </Inputs>
		///    <Outputs>
		///       
		///    </Outputs>
		///    <Returns>
		///       
		///    </Returns>
		///    <Authors>
		///       HungLa
		///    </Authors>
		///    <History>
		///       Wednesday, February 23, 2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		
		public void UpdateDataSet(DataSet pData)
		{
			const string METHOD_NAME = THIS + ".UpdateDataSet()";
			string strSql;
			OleDbConnection oconPCS =null;
			OleDbCommandBuilder odcbPCS ;
			OleDbDataAdapter odadPCS = new OleDbDataAdapter();

			try
			{
				strSql=	"SELECT "
				+ IV_ItemDetailTable.ITEMDETAILID_FLD + ","
				+ IV_ItemDetailTable.OHQUANTITY_FLD + ","
				+ IV_ItemDetailTable.INSPSTATUS_FLD + ","
				+ IV_ItemDetailTable.MRBDISPLAY_FLD + ","
				+ IV_ItemDetailTable.CCNID_FLD + ","
				+ IV_ItemDetailTable.PRODUCTID_FLD + ","
				+ IV_ItemDetailTable.MASTERLOCATIONID_FLD + ","
				+ IV_ItemDetailTable.LOCATIONID_FLD + ","
				+ IV_ItemDetailTable.BINID_FLD + ","
				+ IV_ItemDetailTable.LOT_FLD + ","
				+ IV_ItemDetailTable.SERIAL_FLD 
		+ "  FROM " + IV_ItemDetailTable.TABLE_NAME;

				
				oconPCS = new OleDbConnection(Utils.Instance.OleDbConnectionString);
				odadPCS.SelectCommand = new OleDbCommand(strSql, oconPCS);
				odcbPCS = new OleDbCommandBuilder(odadPCS);
				pData.EnforceConstraints = false;
				odadPCS.Update(pData,IV_ItemDetailTable.TABLE_NAME);

			}
			catch(OleDbException ex)
			{
				if (ex.Errors[1].NativeError == ErrorCode.SQLDUPLICATE_KEYCODE)
				{
																   
					throw new PCSDBException(ErrorCode.DUPLICATE_KEY, METHOD_NAME, ex);				
				}
				else if (ex.Errors[1].NativeError == ErrorCode.SQLCASCADE_PREVENT_KEYCODE)
				{

					throw new PCSDBException(ErrorCode.CASCADE_DELETE_PREVENT, METHOD_NAME, ex);				
				}
				
				else
				{
					throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME,ex);
				}
			}			

			catch(InvalidOperationException ex)
			{
				throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME,ex);
			}
			catch (Exception ex) 
			{
				throw new PCSDBException(ErrorCode.OTHER_ERROR, METHOD_NAME, ex);
			}

			finally 
			{
				if (oconPCS!=null) 
				{
					if (oconPCS.State != ConnectionState.Closed) 
					{
						oconPCS.Close();
					}
				}
			}

		}
	}
}
